Ansible基础篇 - Ansible Playbook用法

一、Anisble-playbook 介绍

1、Playbooks 与 adhoc 相比,是一种完全不同的运用 ansible 的方式,是非常之强大的。

2、Playbook 是由一个或多个 “play” 组成的列表,如果把adhoc比作shell命令的话,Playbook 类似于我们的shell脚本。

3、Playbook采用YAML语言编写。

二、Ansible-playbook 核心元素

Playbook 样例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
---
- hosts: test2
remote_user: root

tasks:
- name: add group nginx
user: name=nginx state=present

- name: add user nginx
user: name=nginx state=present group=nginx

- name: Install nginx
yum: name=nginx state=latest
tags: install

- name: write the nginx config file
template: src=/opt/nginx.conf dest=/etc/nginx/nginx.conf
notify:
- restart nginx
tags: template

- name: ensure nginx is running
service: name=nginx state=restarted
tags: restart

handlers:
- name: restart nginx
service: name=nginx state=restarted

主机和用户名(hosts&&user)

我们可以为playbook中每一个play,个别的选择操作的目标地址是哪些,以哪个用户身份去完成要执行的步骤。

1
2
3
---
- hosts: test2
remote_user: root

执行的任务(tasks)

我们要在远程主机执行的操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
tasks:  
- name: Install nginx
yum: name=nginx state=latest
tags: install

- name: write the nginx config file
template: src=/opt/nginx.conf dest=/etc/nginx/nginx.conf
notify:
- restart nginx
tags: template

- name: ensure nginx is running
service: name=nginx state=restarted
tags: restart

响应事件(handler)

当资源发生变化时采取的一定操作,notify这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时每次都执行指定的操作。

1
2
3
4
5
6
7
8
9
# 当 nginx.conf 发生改变时,会执行handler。
- name: write the nginx config file
template: src=/opt/nginx.conf dest=/etc/nginx/nginx.conf
notify:
- restart nginx # 需要和 handlers 中 name 对应起来

handlers:
- name: restart nginx
service: name=nginx state=restarted

标签(tags)

指定某条任务执行,用于选择运行playbook中的部分代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
- name: Install nginx
yum: name=nginx state=latest
tags: install

- name: write the nginx config file
template: src=/opt/nginx.conf dest=/etc/nginx/nginx.conf
notify:
- restart nginx
tags: template

- name: ensure nginx is running
service: name=nginx state=restarted
tags: restart

  • 当我们执行时:

1、不加任何 tags 参数时,会执行所有标签对应的任务。
2、可以使用 tags 指定需要执行的指定任务:

1
$ ansible-playbook -i hosts hello.yml --tags install

3、可以使用 skip-tags 指定需要跳过执行的任务:

1
$ ansible-playbook -i hosts hello.yml --skip-tags install

  • 特殊的tags:

1、always
当我们在 yaml 中指定了 always 标签后,在执行 playbook 的时候,无论有没有指定这个 tag,带有 always 的 task 都会执行。

2、tagged
我们可以使用 --tags tagged 来执行所有标记了标签的 task,不管标记的标签名字是什么。

1
$ ansible-playbook -i hosts hello.yml --tags tagged

3、untagged
我们可以使用 --tags untagged 来执行所有没有标记标签的task,不管标记的标签名字是什么

1
$ ansible-playbook -i hosts hello.yml --tags untagged

4、我们可以使用 --tag all 来执行所有的任务。

1
$ ansible-playbook -i hosts hello.yml --tags all

三、运行Ansible Playbook

运行方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
运行playbook的方式
ansible-playbook <filename.yml> ... [options]

常见选项
--check -C 只检测可能会发生的改变,但不真正执行操作
(只检查语法,如果执行过程中出现问题,-C无法检测出来)
(执行playbook生成的文件不存在,后面的程序如果依赖这些文件,也会导致检测失败)
--list-hosts 列出运行任务的主机
--list-tags 列出tag (列出标签)
--list-tasks 列出task (列出任务)
--limit 主机列表 只针对主机列表中的主机执行
-v -vv -vvv 显示过程

示例
ansible-playbook hello.yml --check 只检测
ansible-playbook hello.yml --list-hosts 显示运行任务的主机
ansible-playbook hello.yml --limit websrvs 限制主机

执行过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ ansible-playbook -i hosts hello.yml
PLAY [test2] *******************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************************************************
ok: [10.15.3.36]

TASK [add group nginx] *********************************************************************************************************************************************************************************************************************************************************
ok: [10.15.3.36]

TASK [add user nginx] **********************************************************************************************************************************************************************************************************************************************************
ok: [10.15.3.36]

TASK [Install nginx] ***********************************************************************************************************************************************************************************************************************************************************
ok: [10.15.3.36]

TASK [write the nginx config file] *********************************************************************************************************************************************************************************************************************************************
changed: [10.15.3.36]

TASK [ensure nginx is running] *************************************************************************************************************************************************************************************************************************************************
changed: [10.15.3.36]

RUNNING HANDLER [restart nginx] ************************************************************************************************************************************************************************************************************************************************
changed: [10.15.3.36]

PLAY RECAP *********************************************************************************************************************************************************************************************************************************************************************
10.15.3.36 : ok=7 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Playbook run took 0 days, 0 hours, 0 minutes, 5 seconds

执行状态说明

  • 黄色:成功执行并伴随着状态的改变
  • 绿色:成功执行并且没有发生状态的变化
  • 红色:执行失败

四、如何写好一个 YAML 文件

1、大小写敏感;

2、使用缩进表示层级关系;

3、缩进时不允许使用Tab键,只允许使用空格;

4、缩进的空格数目不重要,只要相同层级的元素左侧对齐即可;

5、#表示注释,只支持单行注释,不支持多行注释。

6、一个name只能包含一个task。

7、YAML文件扩展名通常为yml或者yaml

参考文档

Ansible Playbook中的tags